<?php


namespace app\admin\controller\kb;

use app\common\controller\Adminbase;
use think\Db;

class Boardsq extends Adminbase
{
//    protected $searchFields = 'id,username,nickname';

    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $filter = input('filter');
            $where[] = ['bs.is_del', '=', 0];
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['id'])) {
                    $where[] = ['bs.id', '=', $filter['id']];
                }
                if (isset($filter['shop_name'])) {
                    $where[] = ['s.shop_name', 'like', '%' . $filter['shop_name'] . '%'];
                }

                if (isset($filter['status'])) {
                    $where[] = ['bs.status', '=', $filter['status']];
                }

                if (isset($filter['create_time'])) {
                    $create_time = explode(' - ', $filter['create_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['bs.create_time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['express_time'])) {
                    $create_time = explode(' - ', $filter['express_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['bs.express_time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['sh_time'])) {
                    $create_time = explode(' - ', $filter['sh_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['bs.sh_time', 'between', [$create_time[0], $create_time[1]]];
                }

                if (isset($filter['sh_time'])) {
                    $create_time = explode(' - ', $filter['sh_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['bs.sh_time', 'between', [$create_time[0], $create_time[1]]];
                }
            }
            $_list = Db::name('bk_sq')
                ->alias('bs')
                ->join('shop s','bs.shop_id=s.id')
                ->where($where)
                ->field('bs.*,s.shop_name')
                ->page($page, $limit)
                ->order('bs.create_time desc')
                ->select();
            foreach ($_list as $k => $v){
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                if ($v['sh_time']) {
                    $_list[$k]['sh_time'] = date('Y-m-d H:i:s', $v['sh_time']);
                } else {
                    $_list[$k]['sh_time'] = '------';
                }
                if ($v['express_time']) {
                    $_list[$k]['express_time'] = date('Y-m-d H:i:s', $v['express_time']);
                } else {
                    $_list[$k]['express_time'] = '------';
                }
            }
            $total = Db::name('bk_sq')
                ->alias('bs')
                ->join('shop s','bs.shop_id=s.id')
                ->where($where)->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        return $this->fetch();
    }

    /**
     * 状态
     */
    public function setstate()
    {
        $id = $this->request->param('id/d', 0);

        $status = $this->request->param('value/d');

        if (Db::name('bk_sq')->where('id', $id)->update(['status' => $status])) {
            $this->success('操作成功！');
        }
        $this->error('操作失败！');
    }

    //删除
    public function del()
    {

        $ids = $this->request->param('ids/a', null);

        try {
            foreach ($ids as $id) {
                Db::name('bk_sq')->where('id', $id)->delete();
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('删除成功！');
    }
    //删除
    public function refuse()
    {

        $ids = $this->request->param('ids/a', null);

        try {
            foreach ($ids as $id) {
                Db::name('bk_sq')->where('id', $id)->update([
                    'id'=>$id,
                    'status'=>2
                ]);
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('删除成功！');
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            $update_data = array(
                'id' => $data['id'],
                'title' => $data['title'],
                'img' => $data['img'],
                'type' => $data['type'],
                'sort' => $data['sort'],
            );
            $stmt = Db::name('bk_sq')->update($update_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
            return $data;
        }
        $id = $this->request->param('id/d', 0);
        $data = Db::name('bk_sq')->find($id);
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function send2()
    {   $express = Db::name('express')
        ->where(['is_del' => 0, 'status' => 1])
        ->order('sort desc')
        ->select();
        $id = $this->request->param('id/d', 0);
        $data = Db::name('bk_sq')->find($id);
        $this->assign([
                'data' => $data,
                'express' => $express
            ]
        );
        return $this->fetch();
    }

    public function send()
    {
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            $update_data = array(
                'id' => $data['row']['id'],
                'status' => 1,
                'express_time' =>time(),
                'express_name' =>$data['row']['express_name'],
                'express_no' =>$data['row']['express_no'],
            );
            $stmt = Db::name('bk_sq')->update($update_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        }

        $express = Db::name('express')
        ->where(['is_del' => 0, 'status' => 1])
        ->order('sort desc')
        ->select();
        $id = $this->request->param('id/d', 0);
        $data = Db::name('bk_sq')->find($id);
        $this->assign([
                'data' => $data,
                'express' => $express
            ]
        );
        return $this->fetch();
    }




}